'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
main
set doc = nothing
sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	'// TODO
	args = "o"'lcase(trim(args))
	if args ="" then exit sub
	
	dim doc_parent,pcb_path,prj_path,bak_path	
	pcb_path = doc.path
	dim s,ls,i
	s = split(pcb_path,"\")
	ls = ubound(s)-lbound(s)+1
	for i=0 to ls-5
		doc_parent = doc_parent & s(i) & "\"
	next
	prj_path = doc_parent & s(ls-4) & "\" & s(ls-3)
	bak_path = doc_parent & s(ls-4) & "\bak"
	
	dim usrpath
	Dim fso:Set fso = CreateObject("Scripting.FileSystemObject") 
	select case args	
		case "root":usrpath = root
		case "xs":usrpath = root & "\skill"
		case "env":usrpath = root & "\env"
		case "cam":usrpath = root & "\config\cam"
		case "gerber":usrpath = root & "\config\gerber"
		case "dxfas":usrpath = root & "\config\dxfas"
		case "dc":usrpath = root & "\config\dc"
		case "efm":usrpath = root & "\config\efm"
		case "xy":usrpath = root & "\cache\xy"
		case "siyin": usrpath =root & "\data\siyin.png"
		
		case "smt"::usrpath = doc_parent & "smt"
		case "me"::usrpath = doc_parent & "me"
		case "doc"::usrpath = doc_parent & "doc"
		
		case "o"::usrpath = pcb_path & "output"
		case "pcb"::usrpath = pcb_path
		case "p"::usrpath = prj_path
		case "b"::usrpath = bak_path
		case "clip": usrpath = fso.GetSpecialFolder(TemporaryFolder) & "\mgc_icc_clipboard"
		case "clip1": usrpath = fso.GetParentFoldername(fso.GetSpecialFolder(2)) & "\mgc_icc_clipboard"		
		case "app": usrpath = application.path
		case "standard": 			
			s = split(application.fullname,"\SDD_HOME\")
			usrpath = s(0) & "\SDD_HOME\" & "standard"
		case "config": 
			s = split(application.fullname,"\SDD_HOME\")
			usrpath = s(0) & "\SDD_HOME\" & "standard\config\pcb"
			
	end select
	if usrpath="" then exit sub
	
	dim wsh
	set wsh = createobject("wscript.shell")
	wsh.run usrpath
	set wsh = nothing
	set fso = nothing
end sub
